import traceback
import pyautogui
import os
import pyperclip as clip
import json
import pandas as pd
from sqlalchemy import create_engine
import pypinyin
from bs4 import BeautifulSoup
import random
from jsjj.util.configUtil import *
from jsjj.util.dirUtil import *
from jsjj.util.timeUtil import *
from jsjj.util.dbUtil import *
from jsjj.util.emailUtil import *
from jsjj.util.logUtil import *
from jsjj.util.mouseMoveUtil import *

# 所有京东的，都没有做断电续传的
modeName = '京东_POP_行业大盘走势'
# 运行模式: 日模式；周模式；月模式
runMode = '月模式'
# 京东纯自营的大盘数据-行业-行业大盘
# 'D:/360安全浏览器下载'
downloadFilePath = getdownloadFilePath()
deta = 35
waitSecond = 10
# month是抓取上个月的数据
month = str(datetime.datetime.now().month - 1) + "月"
downloadMonth = {'1月': [1428, 341], '2月': [1503, 341], '3月': [1581, 341],
                 '4月': [1428, 392], '5月': [1503, 392], '6月': [1581, 392],
                 '7月': [1428, 444], '8月': [1503, 444], '9月': [1581, 444],
                 '10月': [1428, 496], '11月': [1503, 496], '12月': [1581, 496]}

downloadWeek = {'1': [1437, 359], '2': [1437, 389], '3': [1437, 419],
                '4': [1437, 449], '5': [1437, 479]}

def TopAndLeftMenu():
    pyautogui.FAILSAFE = False

    # 一开始也应该把文件删除，避免一启动就重复录入了。这里没有断电续传的模式，都是从0开始的
    deleteExtendFile('*.xls')

    sleep(6)

    closePOPTOPFIX()

    pyautogui.keyDown('f5')
    pyautogui.keyUp('f5')

    # 顶部行业
    pyautogui.moveTo(1086, 191, 2)
    pyautogui.click()

    sleep(6)

    # 左边行业大盘
    pyautogui.moveTo(443, 336, 2)
    pyautogui.click()

    # 防止bug
    pyautogui.moveTo(725, 258, 2)
    pyautogui.click()

    if runMode == '月模式':
        # 顶层按月查询
        pyautogui.moveTo(1302, 258, 2)

        pyautogui.moveTo(1302, 374, 2)

        pyautogui.moveTo(1426, 374, 2)

        # 选择到月份(6月)
        pyautogui.moveTo(downloadMonth[month][0], downloadMonth[month][1], 2)
        pyautogui.click()
    elif runMode == '日模式':
        # 进去，什么都不要选，默认就是天模式，默认就是抓取昨天
        pass
    elif runMode == '周模式':
        # 顶层按周查询
        pyautogui.moveTo(1313, 257, 2)
        pyautogui.moveTo(1313, 335, 2)

        # 选择到周
        currentWeekInThisMonth = getWeekOfMonth(datetime.datetime.now().year, datetime.datetime.now().month,
                                                datetime.datetime.now().day)

        # fix bug
        pyautogui.moveTo(1414, 335, 2)
        # 动态计算出来，默认上周的数据
        currentWeekInThisMonth = currentWeekInThisMonth - 1
        pyautogui.moveTo(downloadWeek[str(currentWeekInThisMonth)][0], downloadWeek[str(currentWeekInThisMonth)][1], 4)
        pyautogui.click()

    # 关闭月份菜单
    pyautogui.keyDown('esc')
    pyautogui.keyUp('esc')


def LoopMenu(conn):
    # ★其他服务(1)
    sleep(3)
    for i in range(0, 1):
        # ========顶层菜单【客厅家具】=======
        sleep(waitSecond)
        pyautogui.moveTo(1015, 259, 2)
        pyautogui.click()
        # 一级
        pyautogui.moveTo(997, 330 + deta * -1, 2)
        # 二级
        pyautogui.moveTo(1192, 330 + deta * -1, 2)
        # 二级（变化）
        pyautogui.moveTo(1192, 298 + i * deta, 2)
        pyautogui.click()

        sleep(3)
        # 点击页面下载
        pyautogui.moveTo(1518, 351, 2)
        sleep(waitSecond)
        pyautogui.click()

        fixBugPOP()

    # ★卧室家具(9->11)
    for i in range(0, 11):
        # ========顶层菜单【客厅家具】=======
        sleep(waitSecond)
        pyautogui.moveTo(1015, 259, 2)
        pyautogui.click()
        # 一级
        pyautogui.moveTo(997, 330 + deta * 0, 2)
        # 二级
        pyautogui.moveTo(1192, 330 + deta * 0, 2)
        # 二级（变化）
        if i <= 8:
            pyautogui.moveTo(1192, 298 + i * deta, 2)
        elif i == 9:
            pyautogui.scroll(-60)
            pyautogui.moveTo(1192, 298 + i * 7, 2)
        elif i == 10:
            pyautogui.scroll(-60)
            pyautogui.moveTo(1192, 298 + i * 8, 2)
        pyautogui.click()

        sleep(3)
        # 点击页面下载
        pyautogui.moveTo(1518, 351, 2)
        sleep(waitSecond)
        pyautogui.click()

        fixBugPOP()

    # ★客厅家具(9)
    for i in range(0, 9):
        # ========顶层菜单【客厅家具】=======
        sleep(waitSecond)
        pyautogui.moveTo(1015, 259, 2)
        pyautogui.click()
        # 一级
        pyautogui.moveTo(997, 330 + deta * 1, 2)
        # 二级
        pyautogui.moveTo(1192, 330 + deta * 1, 2)
        # 二级（变化）
        pyautogui.moveTo(1192, 298 + i * deta, 2)
        pyautogui.click()

        sleep(3)
        # 点击页面下载
        pyautogui.moveTo(1518, 351, 2)
        sleep(waitSecond)
        pyautogui.click()

        fixBugPOP()

    # ★餐厅家具(5)
    for i in range(0, 5):
        # ========顶层菜单【客厅家具】=======
        sleep(waitSecond)
        pyautogui.moveTo(1015, 259, 2)
        pyautogui.click()
        # 一级
        pyautogui.moveTo(997, 330 + deta * 2, 2)
        # 二级
        pyautogui.moveTo(1192, 330 + deta * 2, 2)
        # 二级（变化）
        pyautogui.moveTo(1192, 298 + i * deta, 2)
        pyautogui.click()

        sleep(3)
        # 点击页面下载
        pyautogui.moveTo(1518, 351, 2)
        sleep(waitSecond)
        pyautogui.click()

        fixBugPOP()

    # ★书房家具(6)
    for i in range(0, 6):
        # ========顶层菜单【客厅家具】=======
        sleep(waitSecond)
        pyautogui.moveTo(1015, 259, 2)
        pyautogui.click()
        # 一级
        pyautogui.moveTo(997, 330 + deta * 3, 2)
        # 二级
        pyautogui.moveTo(1192, 330 + deta * 3, 2)
        # 二级（变化）
        pyautogui.moveTo(1192, 298 + i * deta, 2)
        pyautogui.click()

        sleep(3)
        # 点击页面下载
        pyautogui.moveTo(1518, 351, 2)
        sleep(waitSecond)
        pyautogui.click()

        fixBugPOP()

    # ★储物家具(6)
    for i in range(0, 6):
        # ========顶层菜单【客厅家具】=======
        sleep(waitSecond)
        pyautogui.moveTo(1015, 259, 2)
        pyautogui.click()
        # 一级
        pyautogui.moveTo(997, 330 + deta * 4, 2)
        # 二级
        pyautogui.moveTo(1192, 330 + deta * 4, 2)
        # 二级（变化）
        pyautogui.moveTo(1192, 298 + i * deta, 2)
        pyautogui.click()

        sleep(3)
        # 点击页面下载
        pyautogui.moveTo(1518, 351, 2)
        sleep(waitSecond)
        pyautogui.click()

        fixBugPOP()

    # ★阳台/户外(6)
    for i in range(0, 6):
        # ========顶层菜单【客厅家具】=======
        sleep(waitSecond)
        pyautogui.moveTo(1015, 259, 2)
        pyautogui.click()
        # 一级
        pyautogui.moveTo(997, 330 + deta * 5, 2)
        # 二级
        pyautogui.moveTo(1192, 330 + deta * 5, 2)
        # 二级（变化）
        pyautogui.moveTo(1192, 298 + i * deta, 2)
        pyautogui.click()

        sleep(3)
        # 点击页面下载
        pyautogui.moveTo(1518, 351, 2)
        sleep(waitSecond)
        pyautogui.click()

        fixBugPOP()

    # ★商业办公(9->10)
    for i in range(0, 10):
        # ========顶层菜单【客厅家具】=======
        sleep(waitSecond)
        pyautogui.moveTo(1015, 259, 2)
        pyautogui.click()
        # 一级
        pyautogui.moveTo(997, 330 + deta * 6, 2)
        # 二级
        pyautogui.moveTo(1192, 330 + deta * 6, 2)
        # 二级（变化）
        if i <= 8:
            pyautogui.moveTo(1192, 298 + i * deta, 2)
        elif i == 9:
            pyautogui.scroll(-20)
            pyautogui.moveTo(1192, 298 + i * 8, 2)

        pyautogui.click()

        sleep(3)
        # 点击页面下载
        pyautogui.moveTo(1518, 351, 2)
        sleep(waitSecond)
        pyautogui.click()

        fixBugPOP()

    # ★套房家具(5)
    for i in range(0, 5):
        # ========顶层菜单【客厅家具】=======
        sleep(waitSecond)
        pyautogui.moveTo(1015, 259, 2)
        pyautogui.click()
        # 一级
        pyautogui.moveTo(997, 330 + deta * 7, 2)
        # 二级
        pyautogui.moveTo(1192, 330 + deta * 7, 2)
        # 二级（变化）
        pyautogui.moveTo(1192, 298 + i * deta, 2)
        pyautogui.click()

        sleep(3)
        # 点击页面下载
        pyautogui.moveTo(1518, 351, 2)
        sleep(waitSecond)
        pyautogui.click()

        fixBugPOP()

    # closeF12AtLastStep()

def saveFilesToMysql(conn):
    sleep(5)
    # # 到这里，文件已经全部截取下来了，挨个处理文件入库
    filelist = getRawFileList(downloadFilePath)
    for file in filelist[0]:
        if "行业大盘走势" in file:
            df = pd.read_excel(file, sheet_name=0)
            df = df.dropna(axis=0, how='all')
            df['第几周'] = int(time.strftime("%W")) + 1
            df['导入文件名称'] = file.split('\\')[1]
            df['导入月份'] = month
            df['导入时间'] = datetime.datetime.now()
            df['运行模式'] = runMode
            df['统计月'] = ''
            df['统计周'] = ''
            df['统计日'] = ''
            df['统计日周月'] = ''
            if runMode == '月模式':
                df['统计月'] = str(datetime.datetime.now().month - 1) + "月"
                df['统计日周月'] = df['统计月']
            if runMode == '周模式':
                df['统计周'] = getLastWeekOfYear(datetime.datetime.now().year, datetime.datetime.now().month,datetime.datetime.now().day)
                df['统计日周月'] = df['统计周']
            if runMode == '日模式':
                df['统计日'] = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime(
                    "%Y-%m-%d %H:%M:%S")  # 减一天，统计的是昨天的数据
                df['统计日周月'] = df['统计日']
            # 解决可能出现的超时问题bugfix 2020.08.18
            conn.connection.connection.ping(reconnect=True)
            df.to_sql(name='京东_POP_行业大盘走势', con=conn, if_exists='append', index=False)

    # 全部保存成功了，这里就应该删除本批次的文件，避免和其他批次混起来
    deleteExtendFile('*.xls')

def deleteWholeWrongData(engine):
    # 原则上都是某个模式下的某月，某天的数据全部删除，再重新跑
    strDayWeekMonth = ''
    if runMode == '月模式':
        strDayWeekMonth = str(datetime.datetime.now().month - 1) + "月"
    if runMode == '周模式':
        strDayWeekMonth = getLastWeekOfYear(datetime.datetime.now().year, datetime.datetime.now().month,
                                            datetime.datetime.now().day)
    if runMode == '日模式':
        strDayWeekMonth = strDayWeekMonth(datetime.datetime.now() + datetime.timedelta(days=-1)).strftime(
            "%Y-%m-%d %H:%M:%S")  # 减一天，统计的是昨天的数据
    sql = "delete from " + modeName + " where 运行模式='" + runMode + "' and 统计日周月='" + strDayWeekMonth + "'"
    engine.execute(sql)

def executeCatchWeb(engine, conn):
    try:
        LogTaskAndMachine('京东_POP_行业大盘走势', engine, conn, '', runMode)
        deleteWholeWrongData(engine)
        # 顶部菜单
        TopAndLeftMenu()

        # 循环下面的菜单
        LoopMenu(conn)

        saveFilesToMysql(conn)
    except Exception as e:
        traceback.print_exc()
        sendAlert('【异常中断】' + modeName, '异常:' + str(e) + '|报错文件:' + os.path.split(__file__)[-1], engine, conn)
        print('【异常中断】' + modeName, '异常:' + str(e) + '|报错文件:' + os.path.split(__file__)[-1])
        return
    sendFinalSuccessEmail('●正常完成●' + modeName, '', engine, conn, modeName, runMode)


if __name__ == '__main__':
    # 本函数调试用
    engine, conn = getConn()
    executeCatchWeb(engine, conn)
